Function-Argument Variables

A third type of variable in Explain™'s repertoire is the function argument. You have already seen arguments in the examples in the Global and Local Variables discussions, so let's try an example:

function Main()

ExpSpeed = 1.5

SetSpeed(ExpSpeed)

SetSpeed(1.5)

SetSpeed(NIL)

 

function

SetSpeed(NewValue)

... ; function's code

return

The variable, NewValue, in function SetSpeed() is an argument. It behaves a lot like a local variable in that it is known and reliable only while the interpreter is executing the statements inside the SetSpeed() function. If you call SetSpeed() repeatedly, NewValue is not remembered between calls.

There is another issue to consider when you use arguments. Professional programmers call this the "Call by Value versus Call by Reference" problem. Explain uses a Call by Value for simple data types. This means that the caller function gives the called function a copy of the data value rather than the data itself. It is easier to see this with an example:

function Main()

Speed = 1.5

printl("Main: Speed = ",Speed)

Modify(Speed)

printl("Main: Speed = ",Speed)

return

 

function Modify(Speed)

printl("Modify: Speed = ",Speed)

Speed = Speed + 1.0

printl("Modify: Speed = ",Speed)

return

The output from this program looks like:

Main: Speed = 1.5 Modify: Speed = 1.5 Modify: Speed = 2.5 Main: Speed = 1.5

So the argument Speed in function Modify() is modified, but the original value in function Main() is left alone. Even though they have the same name and origin, they are two completely different values. You can get around this effect by using global variables.

More-complex data objects are implemented as Call by Reference. See an example in the Object Scope and Lifetime section.